<!DOCTYPE html>

















































<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <meta name="description" content="Javadoc API documentation for Dormancy." />

<link rel="shortcut icon" type="image/x-icon" href="../../../favicon.ico" />
<title>

  MapPersister


| Dormancy

</title>
<link href="../../../../assets/doclava-developer-docs.css" rel="stylesheet" type="text/css" />
<link href="../../../../assets/customizations.css" rel="stylesheet" type="text/css" />
<script src="../../../../assets/search_autocomplete.js" type="text/javascript"></script>
<script src="../../../../assets/jquery-resizable.min.js" type="text/javascript"></script>
<script src="../../../../assets/doclava-developer-docs.js" type="text/javascript"></script>
<script src="../../../../assets/prettify.js" type="text/javascript"></script>
<script type="text/javascript">
  setToRoot("../../../", "../../../../assets/");
</script>
<script src="../../../../assets/doclava-developer-reference.js" type="text/javascript"></script>
<script src="../../../../assets/navtree_data.js" type="text/javascript"></script>
<script src="../../../../assets/customizations.js" type="text/javascript"></script>
<noscript>
  <style type="text/css">
    html,body{overflow:auto;}
    #body-content{position:relative; top:0;}
    #doc-content{overflow:visible;border-left:3px solid #666;}
    #side-nav{padding:0;}
    #side-nav .toggle-list ul {display:block;}
    #resize-packages-nav{border-bottom:3px solid #666;}
  </style>
</noscript>
</head>

<body class="">

<div id="header">
    <div id="headerLeft">
    
      <span id="masthead-title">Dormancy</span>
    
    </div>
    <div id="headerRight">
      
  <div id="search" >
      <div id="searchForm">
          <form accept-charset="utf-8" class="gsc-search-box" 
                onsubmit="return submit_search()">
            <table class="gsc-search-box" cellpadding="0" cellspacing="0"><tbody>
                <tr>
                  <td class="gsc-input">
                    <input id="search_autocomplete" class="gsc-input" type="text" size="33" autocomplete="off"
                      title="search developer docs" name="q"
                      value="search developer docs"
                      onFocus="search_focus_changed(this, true)"
                      onBlur="search_focus_changed(this, false)"
                      onkeydown="return search_changed(event, true, '../../../')"
                      onkeyup="return search_changed(event, false, '../../../')" />
                  <div id="search_filtered_div" class="no-display">
                      <table id="search_filtered" cellspacing=0>
                      </table>
                  </div>
                  </td>
                  <td class="gsc-search-button">
                    <input type="submit" value="Search" title="search" id="search-button" class="gsc-search-button" />
                  </td>
                  <td class="gsc-clear-button">
                    <div title="clear results" class="gsc-clear-button">&nbsp;</div>
                  </td>
                </tr></tbody>
              </table>
          </form>
      </div><!-- searchForm -->
  </div><!-- search -->
      
    </div>
</div><!-- header -->


  <div class="g-section g-tpl-240" id="body-content">
    <div class="g-unit g-first side-nav-resizable" id="side-nav">
      <div id="swapper">
        <div id="nav-panels">
          <div id="resize-packages-nav">
            <div id="packages-nav">
              <div id="index-links"><nobr>
                <a href="../../../packages.html"  >Package Index</a> | 
                <a href="../../../classes.html" >Class Index</a></nobr>
              </div>
              <ul>
                
    <li class="api apilevel-">
  <a href="../../../at/dormancy/package-summary.html">at.dormancy</a></li>
    <li class="api apilevel-">
  <a href="../../../at/dormancy/access/package-summary.html">at.dormancy.access</a></li>
    <li class="api apilevel-">
  <a href="../../../at/dormancy/aop/package-summary.html">at.dormancy.aop</a></li>
    <li class="api apilevel-">
  <a href="../../../at/dormancy/persistence/package-summary.html">at.dormancy.persistence</a></li>
    <li class="selected api apilevel-">
  <a href="../../../at/dormancy/persister/package-summary.html">at.dormancy.persister</a></li>
    <li class="api apilevel-">
  <a href="../../../at/dormancy/persister/callback/package-summary.html">at.dormancy.persister.callback</a></li>
    <li class="api apilevel-">
  <a href="../../../at/dormancy/persister/filter/package-summary.html">at.dormancy.persister.filter</a></li>
    <li class="api apilevel-">
  <a href="../../../at/dormancy/persister/function/package-summary.html">at.dormancy.persister.function</a></li>
    <li class="api apilevel-">
  <a href="../../../at/dormancy/persister/predicate/package-summary.html">at.dormancy.persister.predicate</a></li>
    <li class="api apilevel-">
  <a href="../../../at/dormancy/util/package-summary.html">at.dormancy.util</a></li>
              </ul><br/>
            </div> <!-- end packages -->
          </div> <!-- end resize-packages -->
          <div id="classes-nav">
            <ul>
              
    <li><h2>Interfaces</h2>
      <ul>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/DynamicEntityPersister.html">DynamicEntityPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/EntityPersister.html">EntityPersister</a>&lt;C&gt;</li>
      </ul>
    </li>
              
    <li><h2>Classes</h2>
      <ul>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/AbstractContainerPersister.html">AbstractContainerPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/AbstractEntityPersister.html">AbstractEntityPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/AbstractPropertyAccessorPersister.html">AbstractPropertyAccessorPersister</a>&lt;C,&nbsp;PA&nbsp;extends&nbsp;PropertyAccessor&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/ArrayPersister.html">ArrayPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/BeanPropertiesPersister.html">BeanPropertiesPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/BeanPropertyAccessorPersister.html">BeanPropertyAccessorPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/CollectionPersister.html">CollectionPersister</a>&lt;C&nbsp;extends&nbsp;<a href="http://download.oracle.com/javase/6/docs/api/index.html?java/util/Collection.html">Collection</a>&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/DirectFieldAccessorPersister.html">DirectFieldAccessorPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/FieldFilterPersister.html">FieldFilterPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/FunctionPersister.html">FunctionPersister</a>&lt;E&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/GenericEntityPersister.html">GenericEntityPersister</a>&lt;C&gt;</li>
          <li class="selected api apilevel-"><a href="../../../at/dormancy/persister/MapPersister.html">MapPersister</a></li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/NoOpPersister.html">NoOpPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/NoOpPersister.NoOpEntityPersisterHolder.html">NoOpPersister.NoOpEntityPersisterHolder</a></li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/NullPersister.html">NullPersister</a>&lt;C&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/NullPersister.NullPersisterHolder.html">NullPersister.NullPersisterHolder</a></li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/PredicatePersister.html">PredicatePersister</a>&lt;C,&nbsp;P&nbsp;extends&nbsp;Predicate&gt;</li>
          <li class="api apilevel-"><a href="../../../at/dormancy/persister/SimplePersister.html">SimplePersister</a>&lt;C&gt;</li>
      </ul>
    </li>
              
              
              
              
            </ul><br/>
          </div><!-- end classes -->
        </div><!-- end nav-panels -->
        <div id="nav-tree" style="display:none">
          <div id="index-links"><nobr>
            <a href="../../../packages.html"  >Package Index</a> | 
            <a href="../../../classes.html" >Class Index</a></nobr>
          </div>
        </div><!-- end nav-tree -->
      </div><!-- end swapper -->
    </div> <!-- end side-nav -->
    <script>
      if (!isMobile) {
        $("<a href='#' id='nav-swap' onclick='swapNav();return false;' style='font-size:10px;line-height:9px;margin-left:1em;text-decoration:none;'><span id='tree-link'>Use Tree Navigation</span><span id='panel-link' style='display:none'>Use Panel Navigation</span></a>").appendTo("#side-nav");
        chooseDefaultNav();
        if ($("#nav-tree").is(':visible')) {
          init_default_navtree("../../../");
        } else {
          addLoadEvent(function() {
            scrollIntoView("packages-nav");
            scrollIntoView("classes-nav");
          });
        }
        $("#swapper").css({borderBottom:"2px solid #aaa"});
      } else {
        swapNav(); // tree view should be used on mobile
      }
    </script>



<div class="g-unit" id="doc-content">

<div id="api-info-block">



  
   
  
  
  
   
  
  

  
   
  
  
  
   
  
  

  
   
  
  
  
  

  
   
  
  
  
  


<div class="sum-details-links">

<div>
<a href="MapPersister.html">View Documentation</a>
</div>


</div><!-- end sum-details-links -->
<div class="api-level">
  


  
  

</div>
</div><!-- end api-info-block -->


<!-- ======== START OF CLASS DATA ======== -->

<div id="jd-header">
    public
     
     
    
    class
<h1>MapPersister</h1>



  
  
  

  
  
  

  
    extends <a href="../../../at/dormancy/persister/AbstractContainerPersister.html">AbstractContainerPersister</a>&lt;C&gt;<br/>
  
  
  

  
  
  


</div><!-- end header -->
<div id="jd-content">
  <pre class="prettyprint">
/*
 * Copyright 2013 Gregor Schauer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.dormancy.persister;

import at.dormancy.Dormancy;
import org.springframework.core.CollectionFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Processes all types of {@link Map}s by traversing them and invoking the desired operation of the appropriate
 * {@link EntityPersister} for all keys and values.
 *
 * @author Gregor Schauer
 */
public class MapPersister extends AbstractContainerPersister&lt;Map&lt;?, ?&gt;&gt; {
	@Inject
	@SuppressWarnings("unchecked")
	public MapPersister(@Nonnull Dormancy dormancy) {
		super(dormancy);
		supportedTypes = new HashSet&lt;Class&lt;? extends Map&lt;?, ?&gt;&gt;&gt;();
		supportedTypes.add((Class) Map.class);
	}

	@Nullable
	@Override
	public &lt;T extends Map&lt;?, ?&gt;&gt; Map clone_(@Nullable T dbObj, @Nonnull Map&lt;Object, Object&gt; tree) {
		if (dbObj == null || tree.containsKey(dbObj)) {
			return (Map) tree.get(dbObj);
		}
		Map&lt;Object, Object&gt; map = createContainer(dbObj);
		tree.put(dbObj, map);

		for (Map.Entry&lt;?, ?&gt; entry : dbObj.entrySet()) {
			Object key = dormancy.clone_(entry.getKey(), tree);
			Object value = dormancy.clone_(entry.getValue(), tree);
			map.put(key, value);
		}
		return map;
	}

	@Nullable
	@Override
	public &lt;T extends Map&lt;?, ?&gt;&gt; Map&lt;?, ?&gt; merge_(@Nullable T trObj, @Nonnull Map&lt;Object, Object&gt; tree) {
		if (trObj == null || tree.containsKey(trObj)) {
			return (Map&lt;?, ?&gt;) tree.get(trObj);
		}
		Map&lt;Object, Object&gt; map = createContainer(trObj);
		tree.put(trObj, map);

		for (Map.Entry&lt;?, ?&gt; entry : trObj.entrySet()) {
			Object key = dormancy.merge_(entry.getKey(), tree);
			Object value = dormancy.merge_(entry.getValue(), tree);
			map.put(key, value);
		}
		return map;
	}

	@Nullable
	@Override
	@SuppressWarnings("unchecked")
	public &lt;T extends Map&lt;?, ?&gt;&gt; Map&lt;?, ?&gt; merge_(@Nullable T trObj, @Nullable T dbObj, @Nonnull Map&lt;Object, Object&gt; tree) {
		if (trObj == null || dbObj == null || tree.containsKey(trObj)) {
			return (Map&lt;?, ?&gt;) tree.get(trObj);
		}
		Map container = createContainer(dbObj);
		tree.put(trObj, container);

		// Create a modifiable copy of the persistent map
		Map&lt;Object, Object&gt; dbCopy = createContainer(dbObj);
		dbCopy.putAll(dbObj);

		for (Map.Entry&lt;?, ?&gt; trEntry : trObj.entrySet()) {
			// For every transient key, find a persistent element and the associated value
			Object dbKey = trEntry.getKey() != null ? dormancy.getUtils().findPendant(trEntry.getKey(), dbCopy.keySet()) : null;
			Object dbValue = dbKey != null ? dbObj.get(dbKey) : null;

			// Merge the retrieved keys and values (if possible)
			Object mKey = null, mValue = null;
			if (trEntry.getKey() != null) {
				mKey = dbKey != null ? dormancy.merge_(trEntry.getKey(), dbKey, tree) : dormancy.merge_(trEntry.getKey(), tree);
			}
			if (trEntry.getValue() != null) {
				mValue = dbValue != null ? dormancy.merge_(trEntry.getValue(), dbValue, tree) : dormancy.merge_(trEntry.getValue(), tree);
			}

			container.put(mKey, mValue);
		}

		// Add the processed entities to the persistent collection
		dbObj.clear();
		dbObj.putAll(container);
		return dbObj;
	}

	@Nonnull
	@Override
	@SuppressWarnings("unchecked")
	protected Map&lt;Object, Object&gt; createContainer(@Nonnull Map&lt;?, ?&gt; container) {
		return CollectionFactory.createApproximateMap(container, container.size());
	}

	@Override
	public Set&lt;Class&lt;? extends Map&lt;?, ?&gt;&gt;&gt; getSupportedTypes() {
		return supportedTypes;
	}
}

  </pre>
</div>
<div id="footer">
Generated by <a href="http://code.google.com/p/doclava/">Doclava</a>.
</div> <!-- end footer -->

</div> <!-- jd-content -->

</div><!-- end doc-content -->

</div> <!-- end body-content --> 

<script type="text/javascript">
init(); /* initialize doclava-developer-docs.js */
</script>

</body>
</html>
